using System;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Graphics;
using BlueGameComponent;
using Microsoft.Xna.Framework.Input.Touch;
using Microsoft.Phone.Tasks;
using System.Threading;

namespace TouchExplode
{
    class MainMenuScreen : MenuScreen
    {
        static String SoundOnText = "Sound:On";
        static String SoundOffText = "Sound:Off";

        Texture2D background;
        Texture2D title;
        Texture2D moregame;
        TouchMenuEntry switchSoundEntry;
        Animation aniTitle = new Animation();
        Animation openXLive = new Animation();
        int nTick = 0;
        public MainMenuScreen()
            : base("")
        {
            // Create our menu entries.
            TouchMenuEntry startGameMenuEntry = new TouchMenuEntry("Start",Color.Blue);
            TouchMenuEntry highScoreMenuEntry = new TouchMenuEntry("High Score",Color.Yellow);
            TouchMenuEntry helpMenuEntry = new TouchMenuEntry("Help", Color.Green);
            switchSoundEntry = new TouchMenuEntry(GetDisplayEntryText(), Color.Red);
            TouchMenuEntry creditsMenuEntry = new TouchMenuEntry("Credits", Color.Purple);
            TouchMenuEntry exitMenuEntry = new TouchMenuEntry("Exit", Color.Black);
            

            // Hook up menu event handlers.
            startGameMenuEntry.Selected += StartSelected;
            highScoreMenuEntry.Selected += HighScoreSelected;
            helpMenuEntry.Selected += HelpSelected;
            switchSoundEntry.Selected += SwitchSoundSelected;
            creditsMenuEntry.Selected += CreditsSelected;
            exitMenuEntry.Selected += ExitSelect;

            // Add entries to the menu.
            MenuEntries.Add(startGameMenuEntry);
            MenuEntries.Add(highScoreMenuEntry);
            MenuEntries.Add(helpMenuEntry); 
            MenuEntries.Add(switchSoundEntry);
            MenuEntries.Add(creditsMenuEntry); 
            MenuEntries.Add(exitMenuEntry);

            AudioManager.StopMusic();
            //AudioManager.PlayMusic("Sounds/menuMusic");

            EnabledGestures = GestureType.Tap;
        }

        public override void  LoadContent()
        {
 	        base.LoadContent();
            background = ScreenManager.Game.Content.Load<Texture2D>("Textures/backgroud");
            title = ScreenManager.Game.Content.Load<Texture2D>("Textures/title");
            moregame = ScreenManager.Game.Content.Load<Texture2D>("Textures/moregame");
            if ( 0==PhysicsWorld.GetInstance().GetShapeCount() )
            {
                for (int i = 0; i < 10; i++)
                {
                    PhysicsWorld.GetInstance().ProduceShapes();
                }
            }
            aniTitle.Initialize(ScreenManager.Game.Content.Load<Texture2D>("Textures/ani_title"), 2);
            aniTitle.Position = new Vector2(GameConstant.WindowWidth/2,20);
            aniTitle.Active = true;
            aniTitle.Looping = true;

            openXLive.Initialize(ScreenManager.Game.Content.Load<Texture2D>("Textures/XLive"), 2);
            openXLive.Position = new Vector2(GameConstant.WindowWidth - 31, GameConstant.WindowHeight - 31);
            openXLive.Active = true;
            openXLive.Looping = true;
            
        }
       /* protected override void UpdateMenuEntryLocations()
        {
            int menuEntryHorizontalPlacement;
            int menuEntryVerticalPlacement = GameConstants.MainMenuTop;

            // update each menu entry's location in turn
            for (int i = 0; i < MenuEntries.Count; i++)
            {
                float entryWidth = Font.MeasureString(MenuEntries[i].Text).X;
                menuEntryHorizontalPlacement = (int)(GameConstants.MainMenuLeft + maxEntryWidth / 2 - entryWidth / 2);
                MenuEntries[i].Position = new Vector2(menuEntryHorizontalPlacement, menuEntryVerticalPlacement);
                menuEntryVerticalPlacement += GameConstants.MainMenuEntryGap;
            }
          
        }
*/

        public override void Update(GameTime gameTime, bool otherScreenHasFocus, bool coveredByOtherScreen)
        {
            base.Update(gameTime, otherScreenHasFocus, coveredByOtherScreen);
            if( IsActive )
            {
                Game1.AdMgr.SetVisible(true);
                PhysicsWorld.GetInstance().Update(gameTime);
            }
            aniTitle.Update(gameTime);
            if (0==nTick++ % 10)
            {
                openXLive.Update(gameTime);
            }
        }

        public override void Draw(GameTime gameTime)
        {
            SpriteBatch spriteBatch = ScreenManager.SpriteBatch;
            spriteBatch.Begin();
            spriteBatch.Draw(background, new Vector2(0, 0), Color.White);
            DrawTransition(title, new Vector2(GameConstant.WindowWidth / 2 - title.Width / 2, 70), Color.White);
            //DrawTransition(moregame, new Vector2(0,0), Color.White);
            spriteBatch.End();
            PhysicsWorld.GetInstance().Draw(gameTime);
            base.Draw(gameTime);
            spriteBatch.Begin();
            DrawTransition(moregame, new Vector2(0, 0), Color.White);
            //openXLive.Draw(spriteBatch);
            spriteBatch.End();
        }
        private void StartSelected(object sender, EventArgs e)
        {
            ScreenManager.RemoveAllScreen();
            AudioManager.PlaySound("Sounds/Menu_Selection");
        }
        private void HighScoreSelected(object sender, EventArgs e)
        {
            ScreenManager.AddScreen(new HighScoreScreen(),null);
            AudioManager.PlaySound("Sounds/Menu_Selection");
        }
        private void HelpSelected(object sender, EventArgs e)
        {
            ScreenManager.AddScreen(new HelpScreen(), null);
            AudioManager.PlaySound("Sounds/Menu_Selection");
        }
        protected void ExitSelect(object sender, EventArgs e)
        {
            ScreenManager.RemoveAllScreen();
            AudioManager.PlaySound("Sounds/Menu_Selection");
            return;
        }
        protected override void OnCancel(PlayerIndex playerIndex)
        {
            ScreenManager.Game.Exit();
            return;
        }
        public override void OnExit()
        {
            if ("Start" == lastSelectedEnteryText)
            {
                ScreenManager.AddScreen(new GameScreen(), null);
            }
            else if ("Exit" == lastSelectedEnteryText)
            {
                ScreenManager.Game.Exit();
            }
        }
        private void SwitchSoundSelected(object sender, EventArgs e)
        {
            AudioManager.Switch = !AudioManager.Switch;
            if (!AudioManager.Switch)
            {
                AudioManager.StopMusic();
            }
            else
            {
                //AudioManager.PlayMusic("Sounds/menuMusic");
            }
            AudioManager.PlaySound("Sounds/Menu_Selection");
            switchSoundEntry.Text = GetDisplayEntryText();
        }
        private void CreditsSelected(object sender, EventArgs e)
        {
            ScreenManager.AddScreen(new CreditsScreen(), null);
            AudioManager.PlaySound("Sounds/Menu_Selection");
        }
        
        private String GetDisplayEntryText()
        {
            if (AudioManager.Switch)
            {
                return SoundOnText;
            }
            else
            {
                return SoundOffText;
            }
        }

        public override void HandleInput(InputState input)
        {
            base.HandleInput(input);
            if (!IsActive)
            {
                return;
            }

            // Return to the main menu when a tap gesture is recognized
            if (input.Gestures.Count > 0)
            {
                GestureSample sample = input.Gestures[0];
                if (sample.GestureType == GestureType.Tap)
                {
                    if (sample.Position.X >= 0 && sample.Position.X <= 62)
                    {
                        if (sample.Position.Y >= 0 && sample.Position.Y <= 62)
                        {
                            MarketplaceSearchTask search = new MarketplaceSearchTask();
                            search.SearchTerms = GameConstant.String_StudioName;
                            search.Show();
                            Thread.Sleep(1000);
                        }
                    }

                    int x = GameConstant.WindowWidth - 62;
                    int y = GameConstant.WindowHeight - 62;
                    if (sample.Position.X >= x && sample.Position.X <= GameConstant.WindowWidth)
                    {
                        if (sample.Position.Y >= y && sample.Position.Y <= GameConstant.WindowHeight)
                        {
                            /*
                            XliveStartFormEx liveform = new XliveStartFormEx(Game1.livemanager);
                            liveform.Background = Game1.liveBkg;
                            liveform.BackgroundAnimation = true;
                            liveform.AnimationSpeed = 5;
                            liveform.NewgameButton = false;
                            liveform.ContinueButton = false;
                            liveform.ExitButton = false;

                            liveform.Show();
                            Game1.AdMgr.SetVisible(false);
                             * */
                        }
                    }

                }
                //input.Gestures.Clear();
            }
        }

    }


}
